/**
 * gcog - generate code with inlined Groovy code.
 * http://code.google.com/p/gcog/
 * 
 * Copyright 2008, Tobias Hientzsch.
 * 
 * MIT license http://www.opensource.org/licenses/mit-license.php
 * 
 * Original idea and code from Cog code generation tool.
 * http://nedbatchelder.com/code/cog
 *
 * Copyright 2004-2008, Ned Batchelder.
 * 
 */

import junit.framework.TestCase

import static WhiteUtils.*

public class WhiteUtilsTest extends TestCase{
    void testSingleLine() {
        assertEquals('', whitePrefix(['']))
        assertEquals('', whitePrefix([' ']))
        assertEquals('', whitePrefix(['x']))
        assertEquals(' ', whitePrefix([' x']))
        assertEquals('\t', whitePrefix(['\tx']))
        assertEquals('  ', whitePrefix(['  x']))
        assertEquals( ' \t \t', whitePrefix([' \t \tx   ']))
    }
    
    void testMultiLine() {
        assertEquals(whitePrefix(['  x','  x','  x']), '  ')
        assertEquals(whitePrefix(['   y','  y',' y']), ' ')
        assertEquals(whitePrefix([' y','  y','   y']), ' ')
    }
 
    void testBlankLinesAreIgnored() {
        assertEquals(whitePrefix(['  x','  x','','  x']), '  ')
        assertEquals(whitePrefix(['','  x','  x','  x']), '  ')
        assertEquals(whitePrefix(['  x','  x','  x','']), '  ')
        assertEquals(whitePrefix(['  x','  x','          ','  x']), '  ')
    }
    
    void testTabCharacters() {
        assertEquals(whitePrefix(['\timport sys', '', '\tprint sys.argv']), '\t')
    }

    void testDecreasingLengths() {
        assertEquals(whitePrefix(['   x','  x',' x']), ' ')
        assertEquals(whitePrefix(['     x',' x',' x']), ' ')
    }

    
    void testNonTermLine() {
        assertEquals(reindentBlock(''), '')
        assertEquals(reindentBlock('x'), 'x')
        assertEquals(reindentBlock(' x'), 'x')
        assertEquals(reindentBlock('  x'), 'x')
        assertEquals(reindentBlock('\tx'), 'x')
        assertEquals(reindentBlock('x', ' '), ' x')
        assertEquals(reindentBlock('x', '\t'), '\tx')
        assertEquals(reindentBlock(' x', ' '), ' x')
        assertEquals(reindentBlock(' x', '\t'), '\tx')
        assertEquals(reindentBlock(' x', '  '), '  x')
    }
        
    void testSingleLineReindentBlock() {
        assertEquals(reindentBlock('\n'), '\n')
        assertEquals(reindentBlock('x\n'), 'x\n')
        assertEquals(reindentBlock(' x\n'), 'x\n')
        assertEquals(reindentBlock('  x\n'), 'x\n')
        assertEquals(reindentBlock('\tx\n'), 'x\n')
        assertEquals(reindentBlock('x\n', ' '), ' x\n')
        assertEquals(reindentBlock('x\n', '\t'), '\tx\n')
        assertEquals(reindentBlock(' x\n', ' '), ' x\n')
        assertEquals(reindentBlock(' x\n', '\t'), '\tx\n')
        assertEquals(reindentBlock(' x\n', '  '), '  x\n')
    }

    void testRealBlock() {
        assertEquals(
            reindentBlock('\timport sys\n\n\tprint sys.argv\n'),
            'import sys\n\nprint sys.argv\n'
            )
    }

    void testDegenerateCases() {
        assertEquals(commonPrefix([]), '')
        assertEquals(commonPrefix(['']), '')
        assertEquals(commonPrefix(['','','','','']), '')
        assertEquals(commonPrefix(['cat in the hat']), 'cat in the hat')
    }

    void testNoCommonPrefix() {
        assertEquals(commonPrefix(['a','b']), '')
        assertEquals(commonPrefix(['a','b','c','d','e','f']), '')
        assertEquals(commonPrefix(['a','a','a','a','a','x']), '')
    }

    void testUsualCases() {
        assertEquals(commonPrefix(['ab', 'ac']), 'a')
        assertEquals(commonPrefix(['aab', 'aac']), 'aa')
        assertEquals(commonPrefix(['aab', 'aab', 'aab', 'aac']), 'aa')
    }

    void testBlankLine() {
        assertEquals(commonPrefix(['abc', 'abx', '', 'aby']), '')
    }

    void testDecreasingLengthsCommonPrefix() {
        assertEquals(commonPrefix(['abcd', 'abc', 'ab']), 'ab')
    }
        
}
